package org.sxp.common.customDialect;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sxp.common.utils.SpringContextUtils;
import org.sxp.modules.sys.service.SysDictService;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * Created by klaus on 2018/5/18.
 */
public class DictTextProcessor extends AbstractAttributeTagProcessor {
    Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static final String DELIMITER = ",";
    private static final String ATTRIBUTE_NAME = "text";
    private static final int PRECEDENCE = 300;

    protected DictTextProcessor(String dialectPrefix) {
        super(
                TemplateMode.HTML, // 处理thymeleaf 的模型
                dialectPrefix, // 标签前缀名
                null, // No tag name: match any tag name
                false, // No prefix to be applied to tag name
                ATTRIBUTE_NAME, // 标签前缀的 属性 例如：< risk:sansiEncrypt="">
                true, // Apply dialect prefix to attribute name
                PRECEDENCE, // Precedence (inside dialect's precedence)
                true); // Remove the matched attribute afterwards
    }

    @Override
    protected void doProcess(ITemplateContext context,
                             IProcessableElementTag tag, AttributeName attributeName,
                             String attributeValue, IElementTagStructureHandler structureHandler) {
        SysDictService dictService = SpringContextUtils.getBean(SysDictService.class);
//
        final String exper = ThymeleafFacade.getRawValue(tag, attributeName); //获取标签内容表达式
        final String elementCompleteName = tag.getElementCompleteName(); //标签名
//        //添加默认选项
//        String def = "";
//        if(tag.getAttribute("default")!=null){
//            def = tag.getAttributeValue("default");
//        }
//        //添加模型 标签
////        model.add(selectM);
//        String kv[] = exper.split("&");
//        Map<String,Object> params = new HashMap<>();
//        for(String item : kv){
//            String keyValue[] = item.split("=");
//            if(keyValue.length == 2){
//                params.put(keyValue[0], keyValue[1]);
//            }
//        }
//        try{
//            if(params.size() > 0){
//                DictDO dictDO = dictService.list(params).single();
//                structureHandler.setBody(dictDO.getValue(),false);
//                return;
//            }
//        }catch (Exception e){
//            logger.error(e.getMessage(),e);
//        }
//        structureHandler.setBody(def,false);
    }

}
